var domain = "http://www.scihi.cn/gqhgl/";
//var domain = "http://192.168.1.123:8082/gqhgl/";
//用户数据
var userData = localStorage.getItem('userData');
//用户id
var userId = localStorage.getItem("userId") || localStorage.getItem('register_userid');
var sessionId = localStorage.getItem("sessionId") || localStorage.getItem('register_sessionid');
//用户语言
var localLang = (localStorage.getItem('localLang')) ? localStorage.getItem('localLang') : '0';
/*
 * main页面点击事件公用
 * 用于从main页面进来的5个导航条的点击调用
 */
function mainClick(element, params) {
	var clickserid = element.C_OnClick_serverID,
		clickcall = element.onclick,
		clickcallB = element.C_OnClick_callback;
	params['serId.s'] = clickserid;
	var sendJson = commonHttp(params);
	ns.sendHttp(sendJson, eval(clickcallB));
}

//公共openwidow跳转方法 islogin(判断是否需要判断登录)
function commonOpenWindow(url, id, params, isLogin) {
	var urls = "";
	if(isLogin == 1) {
		if(localStorage.getItem('userData') != null && localStorage.getItem('userData') != "" && localStorage.getItem('userData') != undefined) {
			urls = url;
		} else {
			urls = "login.html";
		}
	} else {
		urls = url;
	}
	mui.openWindow({
		url: urls,
		id: id,
		styles: {
			top: '0', //新页面顶部位置
			bottom: '0' //新面底部位置
		},
		show: {
			aniShow: 'slide-in-right',
			duration: '200'
		},
		createNew: true,
		waiting: {
			autoShow: false //自动显示等待框，默认为true
		},
		extras: params
	});
}
//公用获取ID方法;
function getId(id) {
	return typeof id === 'string' ? document.getElementById(id) : id;
};
/**
 * 本项目所用loading效果
 * 在请求报文时候调用showLoading
 * 在报文返回后组装页面html后调用closeLoading
 * 点击loading效果可关闭转圈提示
 */
function showLoading() {
	plus.nativeUI.showWaiting('', {
		style: "black",
		background: "rgba(0,0,0,0)",
		color: '#999',
		padlock: true,
		loading: {
			display: "block"
		}
	});
};

/**
 * ajax 请求的方法
 */
function ajAjax(transCode, ajaxData, succFun, errorFun) {
	$.ajax({
		type: "get",
		data:ajaxData,
		url: domain + transCode,
		dataType: "json",
		success: function(datas, textStaus) {
			if(datas.resCode == "success") {
				succFun(datas);
			} else {
				mui.toast(datas.resMsg)
			}
		},
		error: function(e) {
			errorFun(e.resMsg);
		}
	});
};

/*
 * mui允许滑动的js时间
 */
function scroll() {
	mui('.mui-scroll-wrapper').scroll();
	var slider = mui("#slider");
	slider.slider({
		interval: 1000
	});
}

function closeLoading() {
	plus.nativeUI.closeWaiting();
}

/**
 * 检查字符串是否为合法手机号码
 * @param {String} 字符串
 * @return {bool} 是否为合法手机号码
 */
function isTelPhone(aPhone) {
	var bValidate = RegExp(/^(0|86|17951)?(13[0-9]|15[012356789]|18[0-9]|17[0-9]|19[0-9]|14[57])[0-9]{8}$/).test(aPhone);
	if(bValidate) {
		return true;
	} else
		return false;
}

function isNull(str) {
	if(str == null || str == undefined || str == "" || str == null) {
		return true;
	} else {
		return false;
	}
}

/** 
 * 检查字符串是否为合法电话号码
 * @param {String} 字符串
 * @return {bool} 是否为合法电话号码
 */
function isPhone(aTelphone) {
	if(aTelphone.trim().length != 11) {
		return false;
	}
	var bValidate = RegExp(/^0\d{2,3}-?\d{7,8}$|^\d{7,8}$|^(0|86|17951)?(13[0-9]|15[012356789]|18[0-9]|17[0-9]|14[57])[0-9]{8}$/).test(aTelphone);
	if(bValidate) {
		return true;
	} else {
		return false;
	}
}

/** 
 * 检查字符串是否为合法邮编
 * @param {String} 字符串
 * @return {bool} 是否为合法邮编
 */
function isPostcode(aPostcode) {
	var bValidate = RegExp(/^[1-9][0-9]{5}$/).test(aPostcode);
	if(bValidate) {
		return true;
	} else {
		return false;
	}
}
/** 
 * 检查字符串是否为汉字
 * @param {String} 字符串
 * @return {bool} 是否为汉字
 */
function isUnicode(unicodes) {
	var regChineseChar = RegExp(/^[\u4e00-\u9fa5]+$/).test(unicodes);
	if(regChineseChar) {
		return true;
	} else {
		return false;
	}
}

/** 
 * 检查字符串是否为合法身份证号码
 * @param {String} 字符串
 * @return {bool} 是否为合法身份证号码
 */
function isIdCard(idCard) {
	//15位和18位身份证号码的正则表达式
	var regIdCard = /^(^[1-9]\d{7}((0\d)|(1[0-2]))(([0|1|2]\d)|3[0-1])\d{3}$)|(^[1-9]\d{5}[1-9]\d{3}((0\d)|(1[0-2]))(([0|1|2]\d)|3[0-1])((\d{4})|\d{3}[Xx])$)$/;
	//如果通过该验证，说明身份证格式正确，但准确性还需计算
	if(regIdCard.test(idCard)) {
		if(idCard.length == 18) {
			var idCardWi = new Array(7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2), //将前17位加权因子保存在数组里
				idCardY = new Array(1, 0, 10, 9, 8, 7, 6, 5, 4, 3, 2), //这是除以11后，可能产生的11位余数、验证码，也保存成数组
				idCardWiSum = 0, //用来保存前17位各自乖以加权因子后的总和
				idCardMod,
				idCardLast;
			for(var i = 0; i < 17; i++) {
				idCardWiSum += idCard.substring(i, i + 1) * idCardWi[i];
			}
			idCardMod = idCardWiSum % 11; //计算出校验码所在数组的位置
			idCardLast = idCard.substring(17); //得到最后一位身份证号码
			//如果等于2，则说明校验码是10，身份证号码最后一位应该是X
			if(idCardMod == 2) {
				if(idCardLast == "X" || idCardLast == "x") {
					return true;
				} else {
					return false;
				}
			} else {
				//用计算出的验证码与最后一位身份证号码匹配，如果一致，说明通过，否则是无效的身份证号码
				if(idCardLast == idCardY[idCardMod]) {
					return true;
				} else {
					return false;
				}
			}
		}
	} else {
		return false;
	}
}
/**
 * 防止连续点击导致webview打开出错
 * 注：主要用于打开新窗口时候
 */
var tap_first = null;

function dangerTap() {
	if(!tap_first) {
		tap_first = new Date().getTime();
		setTimeout(function() {
			tap_first = null;
		}, 1000);
		return false;
	} else {
		return true;
	}
}
/*
 * 隐藏其他页面
 * */
function closeOthPage() {
	var alls = plus.webview.all();
	for(var i = 0; i < alls.length; i++) {
		var thisPage = alls[i],
			url = thisPage.getURL();
		url = url.substr(url.lastIndexOf("/") + 1, url.length);
		if("main.html" != url && "index.html" != url && "finance.html" != url && "find.html" != url && "my.html" != url) {
			thisPage.hide();
		}
	}
}
/*
 * 将其他页面关闭
 * */
function shutDownOthPages() {
	var alls = plus.webview.all();
	for(var i = 0; i < alls.length; i++) {
		var thisPage = alls[i],
			url = thisPage.getURL();
		url = url.substr(url.lastIndexOf("/") + 1, url.length);

		if("main.html" != url && "index.html" != url && "finance.html" != url && "find.html" != url && "my.html" != url && "guide.html" != url) {
			plus.webview.close(thisPage);

		}
	}
}

function showOthPage() {
	var alls = plus.webview.all();
	for(var i = 0; i < alls.length; i++) {
		var thisPage = alls[i],
			url = thisPage.getURL();
		url = url.substr(url.lastIndexOf("/") + 1, url.length);
		if("main.html" != url && "index.html" != url && "finance.html" != url && "find.html" != url && "my.html" != url) {
			thisPage.show();
		}
	}
}

function getMainPage() { //获取最后一个main页面,防止因为多次加载main取不到问题(理论上main只加载一次)
	var alls = plus.webview.all(),
		mainPage = "";
	for(var i = 0; i < alls.length; i++) {
		var thisPage = alls[i],
			url = thisPage.getURL();
		url = url.substr(url.lastIndexOf("/") + 1, url.length);
		if("main.html" == url) {
			mainPage = thisPage;
		}
	}
	return mainPage;
}

function isHasSubPage(subUrl) {
	subUrl = subUrl.substr(subUrl.lastIndexOf("/") + 1, subUrl.length);
	var alls = plus.webview.all();
	for(var i = 0; i < alls.length; i++) {
		var thisPage = alls[i],
			url = thisPage.getURL();
		url = url.substr(url.lastIndexOf("/") + 1, url.length);
		if(subUrl == url) {
			return true;
		}
	}
	return false;
};

//设置公共弹出框内容的国际化
if(localLang) {
	var toast_isSame, toast_notSame, toast_acceptRules, toast_notEmpty,
		toast_cancelLogOut, alert_unbind, toast_needCode, toast_needPhone,
		prompt_tips, prompt_title, comfirm_tips, cancelBtn, confirmBtn;
	localLang == 0 ? toast_isSame = "新旧密码不可一致!" : toast_isSame = "Enter the new password twice inconsistent!";
	localLang == 0 ? toast_notSame = "两次新密码输入不一致!" : toast_notSame = "The two new passwords differ!";
	localLang == 0 ? toast_acceptRules = "请阅读并接受《服务条款》!" : toast_acceptRules = "Please read and accpet the Terms of Service!";
	localLang == 0 ? toast_notEmpty = " 输入不能为空!" : toast_notEmpty = "Input fields can't be blank!";
	localLang == 0 ? toast_cancelLogOut = "已取消退出登录!" : toast_cancelLogOut = "Logout canceled";
	localLang == 0 ? alert_unbind = "您还未绑定银行卡，请先绑定!" : alert_unbind = "No bank card is bound,please bind first!";
	localLang == 0 ? toast_needCode = "请输入验证码!" : toast_needCode = " Please enter SMS verification code!";
	localLang == 0 ? toast_needPhone = " 请输入手机号!" : toast_needPhone = " Please enter phone number!";
	localLang == 0 ? prompt_tips = " 手机号码修改成功，是否重新登录?" : prompt_tips = "Password changed sucessfully,re-login?";
	localLang == 0 ? prompt_title = "提示" : prompt_title = "Tips";
	localLang == 0 ? comfirm_tips = "确认退出登录？" : comfirm_tips = "Confirm logout?";
	localLang == 0 ? cancelBtn = "取消" : cancelBtn = "Cancel";
	localLang == 0 ? confirmBtn = "确定" : confirmBtn = "Confirm";
};
/*
 * 页面所有input验证非空
 * @param callback  验证通过的后续函数
 */
function allEmpty(callback) {
	var inputEmptyList = $.map($('input'), function(item, index) {
		if(item.value == '' || item.value == undefined) {
			return item
		}
	});
	if(inputEmptyList.length > 0) {
		//		mui.toast($.i18n.prop('toast_notEmpty'))
		mui.toast(toast_notEmpty)
	} else {
		callback(); //验证通过,后续函数
	}
}

/*
 * 服务条款的选择框点击
 * */
function checkboxClick() {
	mui('body').on('tap', '.clause', function() {
		var clauseLabel = $(this).find($('label'));
		if(clauseLabel.hasClass('checked')) {
			clauseLabel.removeClass('checked')
		} else {
			clauseLabel.addClass('checked')
		}
	})
	//跳转
	mui('body').on('tap', '.clause a', function() {
		window.location.href = 'registerClause.html';
	})
}

/*按钮点击效果公共方法*/
function btnPressShadow(e) {
	$(e).addClass("mui-active");
	setTimeout(function() {
		$(e).removeClass("mui-active");
	}, 150);
};

/*关闭popover方法*/
function closePopover() {
	var flg = true;
	var mask = mui.createMask(function() {
		$(".mui-popover").hide(); //隐藏模态窗口
		$(".mui-backdrop").hide(); //隐藏遮罩层
	});
	mask.close();
}

/*popover选择后更改input value*/
function inputValChange(input, ele) {
	closePopover();
	var selectVal = $(ele).html();
	$(input).val(selectVal);
};
/*
 * 国际化
 * 对应标签加 data-lang='@key'
 * @key为中英文对应key值
 */
function translateLanguage() {
	var lang = (localLang == '1') ? 'en' : 'zh'; //判断中英文
	var ws = plus.webview.all();
	var dataPath;
	for(var i = 0; i < ws.length; i++) {
		var wsUrl = ws[i].id;
		if(wsUrl != 'HBuilder') {
			if(wsUrl == 'pages/headline.html' || wsUrl == 'pages/investigate.html' || wsUrl == 'pages/active.html' || wsUrl == 'pages/mine.html') {
				dataPath = '../libs/i18n/';
			} else if(wsUrl == 'index') {
				dataPath = 'libs/i18n/';
			} else {
				dataPath = '../../libs/i18n/';
			}
		}
	};

	jQuery.i18n.properties({
		name: 'strings', // 资源文件名称
		path: dataPath, // 资源文件所在目录路径
		mode: 'both', // 模式：变量或 Map 
		language: lang, // 对应的语言
		cache: false,
		encoding: 'UTF-8',
		callback: function() {
			$("[data-lang]").each(function() {
				if($(this)[0].tagName == 'INPUT') {
					$(this).attr('placeholder', $.i18n.prop($(this).attr('data-lang')))
				} else {
					$(this).html($.i18n.prop($(this).attr('data-lang')));
				}
			})
		}
	});
};

mui.plusReady(function() {
	translateLanguage();
});

/*
 * 国际化样式修改
 * */
if(localLang == '1') {
	$("label").css({
		'white-space': 'nowrap',
		'font-size': '14px'
	});
	$("input").css({
		'font-size': '14px'
	});
	$(".index .list-group li").css({
		'font-size': '12px',
		'overflow': 'hidden',
		'white-space': 'nowrap',
		'text-overflow': 'ellipsis'
	});
	$(".index .list-group [data-lang]").css({
		'font-size': '12px'
	});
	$(".confirmCardInf .mui-col-xs-4").css({
		'white-space': 'nowrap'
	});
	$("#setPayPwd label").css({
		"cssText": 'width:38% !important;white-space: nowrap;font-size: 14px;'
	});
	$("#setPayPwd label~input").css({
		"cssText": 'width:60% !important;font-size: 14px;'
	});
}